package com.xdtech.hotel.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.thoughtworks.xstream.XStream;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.common.utils.JsonUtil;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.RoomConfigDao;
import com.xdtech.hotel.model.DeviceLink;
import com.xdtech.hotel.model.RoomConfig;
import com.xdtech.hotel.service.DeviceLinkService;
import com.xdtech.hotel.service.RoomConfigService;
import com.xdtech.hotel.vo.DeviceLinkItem;
import com.xdtech.hotel.vo.RoomConfigItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:42:14
 * @since 1.0
 * @see
 */
@Service
public class RoomConfigServiceImpl implements RoomConfigService {
	@Autowired
	private RoomConfigDao roomConfigDao;
	@Autowired
	private DeviceLinkService deviceLinkService;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param entity
	 */
	public void save(RoomConfig entity) {
		roomConfigDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<RoomConfig> entities) {
		roomConfigDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param entity
	 */
	public void delete(RoomConfig entity) {
		roomConfigDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		roomConfigDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param id
	 * @return
	 */
	public RoomConfig get(Long id) {
		return roomConfigDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @return
	 */
	public List<RoomConfig> getAll() {
		return roomConfigDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> roomConfigs = null;
		long rows = 0;
		if (pg!=null) {
			Page<RoomConfig> page = roomConfigDao.findPage(pg,"from RoomConfig order by createTime desc", values);
			roomConfigs = BeanUtils.copyListProperties(
					RoomConfigItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<RoomConfig> roomConfigList = roomConfigDao.find("from RoomConfig order by id desc", values);
			roomConfigs = BeanUtils.copyListProperties(
					RoomConfigItem.class, roomConfigList);
			rows = roomConfigs.size();
		}
		results.put("total", rows);
		results.put("rows", roomConfigs);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateRoomConfig(RoomConfigItem item) {
		RoomConfig roomConfig = null;
		if (item.getId()==null) {
			roomConfig = new RoomConfig();
		}else {
			roomConfig = roomConfigDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(roomConfig, item);
		roomConfigDao.save(roomConfig);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param roomConfigId
	 * @return
	 */
	public RoomConfigItem loadRoomConfigItem(Long roomConfigId) {
		RoomConfig roomConfig = roomConfigDao.get(roomConfigId);
		RoomConfigItem roomConfigItem = new RoomConfigItem();
		BeanUtils.copyProperties(roomConfigItem, roomConfig);
		return roomConfigItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteRoomConfigInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:42:14
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteRoomConfigInfo(List<Long> roomConfigIds) {
		for (Long id : roomConfigIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午8:09:05
	 * @modified by
	 * @param roomConfigItem
	 */
	@Override
	public void saveRoomConfigInfos(RoomConfigItem roomConfigItem) {
		RoomConfig roomConfig = new RoomConfig(roomConfigItem.getId(),roomConfigItem.getRoomType(),roomConfigItem.getConfigInfo(),"");
		XStream xstream = new XStream();
	    String xml = xstream.toXML(roomConfigItem);
		roomConfig.setConfigXml(xml);
		roomConfigDao.save(roomConfig);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午8:31:33
	 * @modified by
	 * @param roomConfigId
	 * @return
	 */
	@Override
	public Map<String, Object> loadRoomConfigUnSelectDeviceLinks(String roomType) {
//		XStream xstream = new XStream(); 
//		RoomConfigItem roomConfigItem = (RoomConfigItem) xstream.fromXML(roomConfig.getConfigXml());
		List<DeviceLink> allDeviceLinks = deviceLinkService.getAll();
		List<DeviceLinkItem> unSelectDeviceLinkItems = new ArrayList<DeviceLinkItem>();
		if (StringUtils.isNotBlank(roomType)) {
			RoomConfig roomConfig = roomConfigDao.getByRoomType(roomType);
			if (roomConfig!=null) {
				RoomConfigItem roomConfigItem = (RoomConfigItem) new XStream().fromXML(roomConfig.getConfigXml());
				Map<String, String> selectIds = new HashMap<String, String>();
				for (DeviceLinkItem deviceLinkItem : roomConfigItem.getDeviceLinks()) {
					selectIds.put(deviceLinkItem.getId().toString(), deviceLinkItem.getId().toString());
				}
				
				for (DeviceLink deviceLink : allDeviceLinks) {
					if (!selectIds.containsKey(deviceLink.getId().toString())) {
						unSelectDeviceLinkItems.add(DeviceLinkItem.createItem(deviceLink));
					}
				}
			}else {
				unSelectDeviceLinkItems = DeviceLinkItem.createItems(allDeviceLinks);
			}
		}else {
			unSelectDeviceLinkItems = DeviceLinkItem.createItems(allDeviceLinks);
		}
		
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("total", unSelectDeviceLinkItems.size());
		results.put("rows",  unSelectDeviceLinkItems);
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午8:31:33
	 * @modified by
	 * @param roomConfigId
	 * @return
	 */
	@Override
	public Map<String, Object> loadRoomConfigSelectDeviceLinks(String roomType) {
		int total = 0;
		List<?> rows = new ArrayList();
		Map<String, Object> results = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(roomType)) {
			RoomConfig roomConfig = roomConfigDao.getByRoomType(roomType);
			if (roomConfig!=null&&StringUtils.isNotBlank(roomConfig.getConfigXml())) {
				XStream xstream = new XStream(); 
				RoomConfigItem roomConfigItem = (RoomConfigItem) xstream.fromXML(roomConfig.getConfigXml());
				total = roomConfigItem.getDeviceLinks().size();
				List<DeviceLinkItem> items = roomConfigItem.getDeviceLinks();
				if (EmptyUtil.isNotEmpty(items)) {
					List<Long> ids = new ArrayList<Long>();
					for (DeviceLinkItem deviceLinkItem : items) {
						ids.add(deviceLinkItem.getId());
					}
					items = deviceLinkService.getDeviceLinkItems(ids);
				}
				
				rows = items;
			}
		}
		results.put("total", total);
		results.put("rows",  rows);
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午10:14:01
	 * @modified by
	 * @param roomType
	 * @return
	 */
	@Override
	public RoomConfig getRoomConfigByRoomType(Long roomConfigId,String roomType) {
		return roomConfigDao.findUnique("from RoomConfig where id!=? and roomType=?", roomConfigId,roomType);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:17:04
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-22下午2:10:54
	 * @modified by
	 * @param roomConfigItem
	 */
	@Override
	public void saveRoomConfigInfoWithLinks(RoomConfigItem roomConfigItem) {
//		deviceLinkService.updateDeviceLinks(roomConfigItem.getDeviceLinks());
		RoomConfig roomConfig = new RoomConfig(roomConfigItem.getId(),roomConfigItem.getRoomType(),roomConfigItem.getConfigInfo(),"");
		
		XStream xstream = new XStream();
	    String xml = xstream.toXML(roomConfigItem);
		roomConfig.setConfigXml(xml);
		roomConfigDao.save(roomConfig);
	}

}
